package com.jianshuge.app.bean;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.jianshuge.app.AppException;
import com.jianshuge.app.bean.MyBooklistDetail.MyBooklistItem;
import com.jianshuge.app.bean.MyBooklistDetail.Reply;
import com.jianshuge.app.common.StringUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Xml;

/**
 * 搜索列表实体类
 */
public class BookItemList extends Entity{

	public final static int CATALOG_ALL = 0;
	public final static int CATALOG_BOOKLIST = 1;
	public final static int CATALOG_BOOK = 2;
	
	private int pageSize;
	private List<BookItem> book_item_list = new ArrayList<BookItem>();
	
	/**
	 * 搜索结果实体类
	 */
	public int getPageSize() {
		return pageSize;
	}
	public List<BookItem> getBookList() {
		return book_item_list;
	}

	
	public static BookItemList parse(InputStream inputStream) throws IOException, AppException {
		
		BookItemList book_item_list = new BookItemList();
		BookItem book_item = null;

		String inputStr;
		BufferedReader streamReader = new BufferedReader( new InputStreamReader(inputStream) ); 
	    StringBuilder responseStrBuilder = new StringBuilder();
	    while ((inputStr = streamReader.readLine()) != null)
	        responseStrBuilder.append(inputStr);
	    String strResult = responseStrBuilder.toString();
	    
		try {
			
			JSONArray json_arr = new JSONArray(strResult);
			
			book_item_list.pageSize = json_arr.length();
			
			for (int i = 0; i < json_arr.length(); i++) {
				
				JSONObject book_item_json_obj = json_arr.getJSONObject(i);
				
				book_item = new BookItem();
				book_item.id = book_item_json_obj.optInt("id");
				book_item.setBookId(book_item_json_obj.optInt("id")) ;
				book_item.setTitle(book_item_json_obj.optString("book_title")) ;
				book_item.setAuthor(book_item_json_obj.optString("book_author"));
				book_item.setPublisher(book_item_json_obj.optString("book_publisher"));
				book_item.setPubdate(book_item_json_obj.optString("book_pubdate"));
				book_item.setIsbn(book_item_json_obj.optString("book_isbn"));
				book_item.setBookRating(book_item_json_obj.optString("book_rating")) ;
				book_item.setBookImageUrl(book_item_json_obj.optString("book_image_url"));
				
				//subject_id is very important
				book_item.setSubjectId(book_item_json_obj.optString("id")) ;
				
				book_item_list.getBookList().add(book_item);
			}
			
		} catch (JSONException e) { 
            System.out.println("Jsons parse error !"); 
            e.printStackTrace(); 
        }
		
	    return book_item_list;
		
		
		
	}
}
